/*
 * 
 * PanjatPinangGLES1
 * State_Menu_Main.java
 * Pratama Wijaya
 * set.mnemonix@gmail.com
 * 11:29:37 PM , Jul 11, 2012
 */
package com.amikomgamedev.panjatpinangGLES1.states;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.SpriteBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.font.FontFactory;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.content.Intent;
import android.graphics.Color;
import android.widget.Toast;

import com.amikomgamedev.panjatpinangGLES1.Config;
import com.amikomgamedev.panjatpinangGLES1.Data;

public class State_Menu_Main extends BaseGameActivity {

	protected Camera mCamera;
	protected Scene mScene;

	private Texture bgTexture;
	private TextureRegion bgTextureReg;
	private Sprite bgSprite;

	private Texture buttonTexture;
	private TextureRegion buttonNormalTexReg;
	private Sprite btnPlay, btnAbout;

	// untuk font
	private Texture fontTex;
	private Font mFont;

	@Override
	public void onLoadComplete() {
		// TODO Auto-generated method stub

	}

	@Override
	public Engine onLoadEngine() {
		// TODO Auto-generated method stub
		mCamera = new Camera(0, 0, Config.GAME_SCREEN_WIDTH,
				Config.GAME_SCREEN_HEIGHT);
		EngineOptions engine = new EngineOptions(true,
				ScreenOrientation.PORTRAIT, new FillResolutionPolicy(), mCamera);
		return new Engine(engine);

	}

	@Override
	public void onLoadResources() {
		// TODO Auto-generated method stub
		bgTexture = new Texture(512, 512,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		buttonTexture = new Texture(512, 512);
		TextureRegionFactory.setAssetBasePath(Data.IMG_MENU_FOLDER_LOCATION);

		bgTextureReg = TextureRegionFactory.createFromAsset(bgTexture, this,
				"bg_menu.png", 0, 0);
		buttonNormalTexReg = TextureRegionFactory.createFromAsset(
				buttonTexture, this, "play_button.png", 0, 0);

		fontTex = new Texture(128, 128);
		mFont = FontFactory.createFromAsset(fontTex, this, Data.FONT_STYLE,
				Data.FONT_SIZE_MEDIUM, true, Color.BLACK);

		this.mEngine.getTextureManager().loadTextures(bgTexture, buttonTexture,
				fontTex);
		this.mEngine.getFontManager().loadFont(mFont);

	}

	@Override
	public Scene onLoadScene() {
		// TODO Auto-generated method stub
		this.mEngine.registerUpdateHandler(new FPSLogger());
		mScene = new Scene(1);

		bgSprite = new Sprite(0, 0, bgTextureReg);
		mScene.setBackground(new SpriteBackground(bgSprite));

		btnPlay = new Sprite(115, 285, buttonNormalTexReg) {

			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				// TODO Auto-generated method stub
				Intent play = new Intent(State_Menu_Main.this,
						State_Gameplay.class);
				startActivity(play);
				return true;
			}

		};
		btnAbout = new Sprite(115, 345, buttonNormalTexReg) {

			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				// TODO Auto-generated method stub
				Toast.makeText(State_Menu_Main.this, "About",
						Toast.LENGTH_SHORT).show();
				return true;
			}

		};

		mScene.getFirstChild().attachChild(btnPlay);
		mScene.getLastChild().attachChild(btnAbout);

		mScene.getLastChild().attachChild(new Text(120, 300, mFont, "Start"));
		mScene.getLastChild().attachChild(new Text(120, 360, mFont, "About"));

		mScene.setTouchAreaBindingEnabled(true);
		mScene.registerTouchArea(btnPlay);
		mScene.registerTouchArea(btnAbout);
		return mScene;
	}

}
